﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Ext.Net;

namespace KamLib.Controls
{
    public static class ExtUilActions
    {

        public static IEnumerable<Button> ToButtons(IEnumerable<UilAction> actions)
        {
            if (actions == null) return null;

            return actions.Select(a => new Button
            {
                Text = a.Title,
                IconCls = a.IconCls,
                Enabled = a.Enabled,
                OnClientClick = GetActionScript(a),
            });
        }


        public static string GetActionScript(UilAction action)
        {
            return String.Concat(
                "Ext.requestEvent({url:'/UilService.asmx/Invoke',eventMaskTarget:this.btnEl.dom,",
                    "params: {key:\"", action.Controller.Key.ToJson(), "\", name:'", action.MID, "', args: ''}",
                    //"success: function(result) { console.log(result); eval(result.Script); }", 
                "});"
            );
        }

    }
}
